package com.dl.study.module.user.service.impl;

import com.dl.study.module.user.dao.UserDao;
import com.dl.study.module.user.domain.dto.UserDto;
import com.dl.study.module.user.domain.dto.UserQueryListDto;
import com.dl.study.module.user.domain.entity.UserEntity;
import com.dl.study.module.user.service.UserService;
import com.dl.study.module.user.strategy.context.HandlerContext;
import com.dl.study.module.user.strategy.handler.AOrderTypeHandler;
import com.dl.study.util.BeanCopyUtil;
import com.dl.study.util.resp.ResponseDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: user
 * @Date: 2021/8/30 21:30
 * @Description:
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    //使用策略模式实现
    @Autowired
    private HandlerContext handlerContext;


    @Override
    public ResponseDTO<Object> create(String serile) {
        int result = userDao.create(serile);
        return ResponseDTO.succ();
    }

    @Override
    public ResponseDTO<Object> add(UserQueryListDto userQueryListDto) {
        UserEntity userEntity = BeanCopyUtil.copy(userQueryListDto, UserEntity.class);
        int result = userDao.insert(userEntity);
        return ResponseDTO.succMsg("添加成功");
    }

    @Override
    public ResponseDTO<Object> orderHandler(UserDto userDto) {
        AOrderTypeHandler orderTypeHandler = handlerContext.getInstance(userDto.getType());
        String result = orderTypeHandler.handler(userDto);
        return ResponseDTO.succData(result);
    }
}
